#Clear R
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("psych", "ggplot2", "interplot", "MASS", "foreign",  "car", "stringr", "stargazer", "xtable", "moments", "lavaan", "dplyr")
ipak(packages)

#Load data
load("Study4_data.Rdata")

### Appendix D.1: Sample characteristics---------------
#Age - many younger folks
table(data$Age)

#sex - lot more woman (2) compared to men (1)
table(data$female)
1457/(1052+1457)

#education - high educated
table(data$Education)
(1431)/(1431+798+280)

#race - mostly white
table(data$non_white)
2009/(2009+500)

### Appendix D.2: Design --- Partisan Social identity Strength---------------

summary(data$pidstrength)
sd(data$pidstrength)
kurtosis(data$pidstrength)
skewness(data$pidstrength)

psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T) #alpha is .88
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T) #alpha is .88

huddy_latent<-'pid =~ NA*huddy1 +huddy2+huddy3+huddy4+huddy5+huddy6+huddy7+huddy8
pid ~~ 1*pid'

fit <- cfa(huddy_latent, ordered=c("huddy1", "huddy2", "huddy3", "huddy4", "huddy5", "huddy6", "huddy7", "huddy8"), data=data)
parameterEstimates(fit, standardized=TRUE)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[-c(9:58), ] 
names(p) <- c("Standardized Factor Loading", "p-value")
p<-xtable(caption = "Study 4 Partisan Social Identity Strength: Standardized Factor Loadings", label = "tab:cfaPID_turk", p)
print(p, type="latex", file="Study4_PID.tex", caption.placement="top")

ggplot(data, aes(x = PSIDstrength)) + geom_histogram(binwidth = 0.01)+theme_bw() +labs(x="Partisan Social Identity Strength", y="Count")
#save results
ggsave("Study4_PID_distribution.pdf",width=8,height=4)

### Appendix D.2: Design --- Cognitive Reflection Test---------------

psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7))),check.keys = T) #alpha is very high
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7))),check.keys = T) #alpha is very high
summary(zero1(data$CRTall))
sd(zero1(data$CRTall),na.rm=T)
kurtosis(data$CRTall, na.rm=T)
skewness(data$CRTall, na.rm=T)


data_crt<-data.frame(data$CRT1,  data$CRT2, data$CRT3, data$CRT4, data$CRT5, data$CRT6, data$CRT7)
poly_values = polychoric(data_crt)
items_polychoric = poly_values$rho 
items_polychoric[lower.tri(items_polychoric)] <- NA #remove lower part of the triangle
p<-xtable(caption = "Study 4 Tetrachoric Correlations Cognitive Reflection Test", label = "tab:corCRTturk", items_polychoric)
names(p) <- c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7")
rownames(p) <- c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7")
print(p, type="latex", file="Study4_CRT.tex", caption.placement="top")
#Histogram of CRT
ggplot(data, aes(x = zero1(CRTall))) + geom_histogram(binwidth = 0.01)+theme_bw()+labs(x="Cognitive Reflection Test", y="Count")
#save results
ggsave("Study4_CRT_distribution.pdf",width=8,height=4)

### Appendix D.2: Design --- Dependent variables -------------
alpha_dv<-data.frame(data$DV_support_rec, data$DV_costs_rec, data$DV_bad_rec)
psych::alpha(alpha_dv)

summary(data$DV_irradiation)
sd(data$DV_irradiation)

summary(data$words)
sd(data$words)

summary(data$Quiz_correct)
sd(data$Quiz_correct)

### Appendix D.3: Results -------------
results<-list()
summary(results[[1]]<-lm(zero1(DV_irradiation)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(results[[2]]<-lm(zero1(DV_irradiation)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(results[[3]]<-lm(zero1(DV_irradiation)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))

stargazer(results[[1]], results[[2]], results[[3]], title="Study 4: Food irradiation support, party cues, reflection and social identity strength", align=TRUE, order=c(1,2,3,4, 13,14,11,12,15,16,17,5,6,7,8,9,10), covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","CRT", "In-party * PSID", "Out-party * PSID", "In-party * CRT", "Out-party * CRT", "PSID * CRT", "In-party * PSID * CRT", "Out-party * PSID * CRT", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes.append = FALSE, 
          star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"),
          notes = "+p<.1; *p<0.05", column.sep.width = "1pt",
          no.space=TRUE,font.size="tiny" , out = "Study4_summary_turk.tex",dep.var.caption = "Policy support",column.labels = c("CRT"), column.separate = c(3), dep.var.labels.include = F,label="tab:stud_turk", digits=2)


## CRT at 0 or -1SD
m0 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

Mturk<-ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(.~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom") + ylim(-.2, .2)
ggsave(Mturk, file="Turk_food.pdf",width=8,height=6)


### Appendix D.3: Exploration of mechanism--------------

data$partycues_food<-1
data$partycues_food[data$InParty==1]=2
data$partycues_food[data$OutParty==1]=3
food<-as.data.frame(data %>%
                      group_by(partycues_food) %>%
                      dplyr::summarize(Mean = mean(Prompt_time_Page_Submit, na.rm=TRUE), SD = sd(Prompt_time_Page_Submit, na.rm=TRUE), Min = min(Prompt_time_Page_Submit, na.rm=TRUE), Max = max(Prompt_time_Page_Submit, na.rm=TRUE)))
food$Cue<-c("No cues", "In-party cue", "Out-party cue")
food<-food[,-c(1)]
food<-food[,c(5, 1,2,3,4)]
food<-xtable(caption = "Study 4 Food Irradiation Experiment: Reading Time", label = "tab:reading_food_Turk", food)
print(food, type="latex", file="Food_read_Turk.tex", caption.placement="top")

reading<-list()
summary(reading[[1]]<-lm(log1p(Prompt_time_Page_Submit)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(reading[[2]]<-lm(log1p(Prompt_time_Page_Submit)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(reading[[3]]<-lm(log1p(Prompt_time_Page_Submit)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))

Words<-list()
summary(Words[[1]]<-lm(log1p(words)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(Words[[2]]<-lm(log1p(words)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(Words[[3]]<-lm(log1p(words)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))

quiz<-list()
summary(quiz[[1]]<-lm(Quiz_correct~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(quiz[[2]]<-lm(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(quiz[[3]]<-lm(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))

stargazer(reading[[1]], reading[[2]], reading[[3]], Words[[1]], Words[[2]], Words[[3]], quiz[[1]], quiz[[2]], quiz[[3]], title="Study 4 Reading, Words and Quiz: Food irradiation support, party cues, reflection and social identity strength", align=TRUE, order=c(1,2,3,4, 13,14,11,12,15,16,17,5,6,7,8,9,10), covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","CRT", "In-party * PSID", "Out-party * PSID", "In-party * CRT", "Out-party * CRT", "PSID * CRT", "In-party * PSID * CRT", "Out-party * PSID * CRT", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"),
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes.append = FALSE, 
          star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"),
          notes = "+p<.1; *p<0.05", column.sep.width = "1pt",
          no.space=TRUE,font.size="tiny" , out = "MTurk_words.tex",dep.var.caption = "Mechanism",column.labels = c("Reading time", "Words", "Quiz correct"), column.separate = c(3, 3, 3), dep.var.labels.include = F,label="tab:turk_words", digits=2)

#reading time
## CRT at 0 or -1SD
m0 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("Reading\n time")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#words
## CRT at 0 or -1SD
m0 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_words <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_words$battery      <- c("Words")
forplot_words$CRT <- factor(forplot_words$CRT,levels = c("-1 SD","Mean","+1 SD"))

#Quiz
## CRT at 0 or -1SD
m0 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_Quiz <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_Quiz$battery      <- c("Quiz")
forplot_Quiz$CRT <- factor(forplot_Quiz$CRT,levels = c("-1 SD","Mean","+1 SD"))

forpaper <- rbind(forplot, forplot_words, forplot_Quiz)
forpaper$battery <- factor(forpaper$battery,levels = c("Reading\n time","Words","Quiz"))
turk_mech<-ggplot(forpaper,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom") 
ggsave(turk_mech, file="Turk_mechanism.pdf",width=8,height=6)



### Appendix D.4: Pre-registered robustness checks--- Balance checks--------
balance_crt<-lm(zero1(CRTall)~as.factor(data$treatment), data=data)
balance_crt2<-lm(zero1(CRTall)~InParty + OutParty, data=data)

stargazer(balance_crt, balance_crt2, title="Study 4 Food Irradiation Experiment: balance checks of moderators", align=TRUE, 
          covariate.labels=c("Democrats support", "Republicans support", "In-party cue", "Out-party cue"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes.append = FALSE, 
          notes = "*p<0.05", 
          no.space=TRUE,out = "Study4Balance.tex",dep.var.labels =c("CRT"),label="tab:Turkbalance", digits=2)

#covariates per condition
balanceSex1<-glm(data$female~as.factor(data$treatment), data=data, family=binomial(link="logit"))
balanceAge1<-lm(data$Age~as.factor(data$treatment),data=data)
balanceEdu1<-polr(as.factor(data$Education)~as.factor(data$treatment), data=data, Hess=TRUE)
balanceRace1<-glm(data$non_white~as.factor(data$treatment),data=data, family=binomial(link="logit"))
balanceParty1<-glm(data$Republican_dummy~as.factor(data$treatment),data=data, family=binomial(link="logit"))

#Extract results for models
stargazer(balanceSex1, balanceAge1, balanceEdu1, balanceRace1, balanceParty1, title="Study 4 Food Irradiation Experiment: Balance Checks Demographics", align=TRUE, 
          dep.var.labels=c("Sex", "Age", "Education", "Non-white", "Republican Party"), 
          covariate.labels=c("Democrats support", "Republicans support"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "*p<0.05", 
          notes.append = FALSE, 
          no.space=TRUE,out = "Study4BalanceDem_1.tex", label="tab:balanceDemstudy4_Turk", digits=2)

#covariates
balanceSex<-glm(data$female~InParty+OutParty, data=data, family=binomial(link="logit"))
balanceAge<-lm(data$Age~InParty+OutParty,data=data)
balanceEdu<-polr(as.factor(data$Education)~InParty+OutParty, data=data, Hess=TRUE)
balanceRace<-glm(data$non_white~InParty+OutParty,data=data, family=binomial(link="logit"))
balanceParty<-glm(data$Republican_dummy~InParty+OutParty,data=data, family=binomial(link="logit"))

#Extract results for models
stargazer(balanceSex, balanceAge, balanceEdu, balanceRace, balanceParty, title="Study 4 Food Irradiation Experiment: Balance Checks Demographics", align=TRUE, 
          dep.var.labels=c("Sex", "Age", "Education", "Non-white", "Republican Party"), 
          covariate.labels=c("In-party cue", "Out-party cue"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "*p<0.05", 
          notes.append = FALSE, 
          no.space=TRUE,out = "Study4BalanceDem.tex", label="tab:balanceDemstudy4_turk", digits=2)

### Appendix D.4: Pre-registered robustness checks--- Results item-by-item---------

## CRT at 0 or -1SD
m0 <- lm(zero1(DV_support_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_support_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_support_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("Support")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#2. Costas
## CRT at 0 or -1SD
m0 <- lm(zero1(DV_costs_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_costs_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_costs_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_words <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_words$battery      <- c("Costs vs.\n benefits")
forplot_words$CRT <- factor(forplot_words$CRT,levels = c("-1 SD","Mean","+1 SD"))

#3 Bad 
## CRT at 0 or -1SD
m0 <- lm(zero1(DV_bad_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_bad_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_bad_rec)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_Quiz <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_Quiz$battery      <- c("Good vs.\n Bad")
forplot_Quiz$CRT <- factor(forplot_Quiz$CRT,levels = c("-1 SD","Mean","+1 SD"))

forpaper <- rbind(forplot, forplot_words, forplot_Quiz)
forpaper$battery <- factor(forpaper$battery,levels = c("Support","Costs vs.\n benefits","Good vs.\n Bad"))
turk_item<-ggplot(forpaper,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom") 
ggsave(turk_item, file="Turk_item.pdf",width=8,height=6)

### Appendix D.4: Pre-registered robustness checks--- Check linearity---------

data$PSIDstrength_cats<-NA
data$PSIDstrength_cats[data$PSIDstrength<.4167]=0
data$PSIDstrength_cats[data$PSIDstrength>.4167 & data$PSIDstrength<.5834]=1
data$PSIDstrength_cats[data$PSIDstrength>=.5834]=2
data$PSIDstrength_cats <- factor(data$PSIDstrength_cats,levels = c(0,1,2), label=c("low", "modest", "high"))

# CRT at 0 or -1SD
m0 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength_cats+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength_cats + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength_cats')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength_cats')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength_cats+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength_cats + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength_cats')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength_cats')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength_cats+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength_cats + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength_cats')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength_cats')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#remove lines that are not necessary
forplot <- forplot[-c(3, 7,11,15,19,23), ] 
forplot$fake<-rep(1:3,6)
forplot$fake <- factor(forplot$fake,levels = c("1","2","3"))

Mturk_lin<-ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effects of Party Cues on Policy Support ")+geom_pointrange(aes(ymin=lb,ymax=ub, fill=Cue),alpha=1, position=position_dodge(width=0.2))+geom_hline(yintercept = 0,lty="dashed")+scale_colour_manual(values = c("dark green", "red")) +scale_x_discrete(labels = c("1" = "Weak","2" = "Modest", "3"="Strong"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom")+ylim(-.2,.2)
ggsave(Mturk_lin, file="Turk_lin.pdf",width=8,height=6)

### Discussion
summary(data$CRT7_3)
sd(data$CRT7_3)

### Appendix F: 
cor.test(data$PSIDstrength, data$pidstrength)

## CRT at 0 or -1SD
m0 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*pidstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength + Age + female + non_white + as.factor(Education) + Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$fake <- factor(forplot$fake,levels = c("0","0.5","1"))
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

Mturk<-ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+facet_grid(.~CRT)+xlab("Party Identity Strength")+theme_bw()+ylab("Marginal effect of In-party cue and Out-party cue on Policy Support ")+geom_pointrange(aes(ymin=lb,ymax=ub, fill=Cue),alpha=1)+geom_hline(yintercept = 0,lty="dashed")+scale_colour_manual(values = c("dark green", "red"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom") + scale_x_discrete(labels = c("0" = "Not","0.5" = "Weak", "1"="Strong"))

ggsave(Mturk, file="study4_Mturk.pdf",width=8,height=6)

### Appendix G: Partisanship and policy preferences in the control conditions--------------------
app_H<-list()
summary(app_H[[1]]<-lm(zero1(DV_irradiation)~Republican_dummy,data=subset(data,treatment==1)))
summary(app_H[[2]]<-lm(zero1(DV_irradiation)~partisanship,data=subset(data,treatment==1)))

stargazer(app_H[[2]], app_H[[1]], title="Study 4: Partisanship and policy preferences in the Control Conditions of the Food Irradiation Experiment", align=TRUE, covariate.labels=c("Partisanship (6-point)", "Republican (Ref. Democrat)"), omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes.append = FALSE, 
          star.cutoffs=c(0.05), star.char = c("*"),
          notes = "*p<0.05", column.sep.width = "1pt",
          no.space=TRUE, out = "Study4Mturk_control.tex",dep.var.caption = "Policy support", dep.var.labels.include = F,label="tab:stud_turk_H", digits=2)

### Not in Online Appendix: Results without covariates-------------------

results<-list()
summary(results[[1]]<-lm(zero1(DV_irradiation)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall),subset(data)))
#Two-way
summary(results[[2]]<-lm(zero1(DV_irradiation)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength),data))
#Three way
summary(results[[3]]<-lm(zero1(DV_irradiation)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) ,data))

## CRT at 0 or -1SD
m0 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(zero1(DV_irradiation)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength ,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

Mturk<-ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(.~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom") + ylim(-.2, .2)
ggsave(Mturk, file="Turk_food_nocovariates.pdf",width=8,height=6)

#reading time

reading<-list()
summary(reading[[1]]<-lm(log1p(Prompt_time_Page_Submit)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall),subset(data)))
#Two-way
summary(reading[[2]]<-lm(log1p(Prompt_time_Page_Submit)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength),data))
#Three way
summary(reading[[3]]<-lm(log1p(Prompt_time_Page_Submit)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) ,data))

Words<-list()
summary(Words[[1]]<-lm(log1p(words)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall),subset(data)))
#Two-way
summary(Words[[2]]<-lm(log1p(words)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength),data))
#Three way
summary(Words[[3]]<-lm(log1p(words)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) ,data))

quiz<-list()
summary(quiz[[1]]<-lm(Quiz_correct~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall),subset(data)))
#Two-way
summary(quiz[[2]]<-lm(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength),data))
#Three way
summary(quiz[[3]]<-lm(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall),data))

#reading time
## CRT at 0 or -1SD
m0 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength ,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(log1p(Prompt_time_Page_Submit)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength ,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("Reading\n time")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#words
## CRT at 0 or -1SD
m0 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(log1p(words)~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_words <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_words$battery      <- c("Words")
forplot_words$CRT <- factor(forplot_words$CRT,levels = c("-1 SD","Mean","+1 SD"))

#Quiz
## CRT at 0 or -1SD
m0 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
summary(m2)
m_0_out <- interplot(m0,var1 = "OutParty",var2='PSIDstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='PSIDstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*PSIDstrength,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='PSIDstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='PSIDstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(Quiz_correct~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*PSIDstrength,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='PSIDstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='PSIDstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_Quiz <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_Quiz$battery      <- c("Quiz")
forplot_Quiz$CRT <- factor(forplot_Quiz$CRT,levels = c("-1 SD","Mean","+1 SD"))

forpaper <- rbind(forplot, forplot_words, forplot_Quiz)
forpaper$battery <- factor(forpaper$battery,levels = c("Reading\n time","Words","Quiz"))
turk_mech<-ggplot(forpaper,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom") 
ggsave(turk_mech, file="Turk_mechanism_nocovariates.pdf",width=8,height=6)



### Not in Online Appendix: Models for the Quiz Results using Negative Binomial Regression-----------------

quiz<-list()
summary(quiz[[1]]<-glm.nb(Quiz_correct~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(quiz[[2]]<-glm.nb(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(quiz[[3]]<-glm.nb(Quiz_correct~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))



### Not in Online Appendix: Ranked reading time ----------------
data$time_ranked<-rank(data$Prompt_time_Page_Submit,ties.method = "average")
reading_rank<-list()
summary(reading_rank[[1]]<-lm(zero1(time_ranked)~InParty + OutParty + zero1(PSIDstrength) + zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,subset(data)))
#Two-way
summary(reading_rank[[2]]<-lm(zero1(time_ranked)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))
#Three way
summary(reading_rank[[3]]<-lm(zero1(time_ranked)~InParty+OutParty + zero1(PSIDstrength)+zero1(CRTall)+InParty*zero1(CRTall)+OutParty*zero1(CRTall) +InParty*zero1(PSIDstrength)+OutParty*zero1(PSIDstrength) + zero1(CRTall)*zero1(PSIDstrength)+InParty*zero1(PSIDstrength)*zero1(CRTall)+OutParty*zero1(PSIDstrength)*zero1(CRTall) + Age + female + non_white + as.factor(Education) + Republican_dummy,data))


